<?php

/*
 * Action
 * 
 * +$2
 * 
 * While this is in play, when you buy a card, gain a card costing less than it that is not a Victory card.
 * 
 * 
 * TODO: test this with throne room
 */

class Haggler extends Card implements iKingdom, iHinterlands, iAction, iReactingCard {

    public function __construct() {
        parent::__construct("Haggler", 5);

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Buy);
        $this->reactConditions->actor = array(Yourself);
        $this->reactConditions->cardLocation = array(PlayArea);
        $this->reactConditions->limit = ForEachCard;
        $this->reactConditions->isMandatory = true;
    }

    public function getDescription() {
        return array(
            "+$2.",
            "While this is in play, when you buy a card, gain a card costing less than it that is not a Victory card.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addMoney(new Price(2));
        ScreenDisplay::addScreenMessage($game, true, "get +2$", "gets +2$");
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        return true;
    }

    public function react(Game $game, $card, $name) {
        // get relevant cards
        $maxPrice = $card->getPrice($game)->add(-1);
        $cards = $game->getBoardCardsByPrice(new Price(), $maxPrice);
        $cardsWithoutVictory = array();
        foreach ($cards as $card) {
            if (!($card instanceof iVictory)) {
                $cardsWithoutVictory[] = $card;
            }
        }

        // let the player choose the card
        if (sizeof($cardsWithoutVictory) > 0) {
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $cardsWithoutVictory, 1, 1, null, "Choose a card to gain");
            $card_name = $output[0];
            $card = $game->moveCardFromPileToPile($card_name, BoardCards, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, "gain  " . nicePrint($card), "gains " . ScreenDisplay::$LMSG);
        }
        $game->setTempCard($card);
        return true;
    }

}

?>
